﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimpleCrawler
{
    public partial class Form2 : Form
    {
        Form1 rootForm; 
        public Form2(Form1 form)
        {
            InitializeComponent();
            rootForm = form; 

            label1.Text = "Totally " + rootForm.totalUrls.Count + " URLs crawled";

            // calculate domain
            Dictionary<string, int> domains = new Dictionary<string,int>();
            for (int i = 0; i < rootForm.totalUrls.Count; i++)
            {
                int s = rootForm.totalUrls[i].IndexOf("//");
                if (s == -1) continue;
                s += 2;
                int e = rootForm.totalUrls[i].IndexOf("/", s);
                if (e == -1) e = rootForm.totalUrls[i].Length;
                string domain = rootForm.totalUrls[i].Substring(s, e - s);
                if (domains.ContainsKey(domain))
                    domains[domain]++;
                else
                    domains.Add(domain, 1);
            }

            label2.Text = "Totally " + domains.Count + " domains found";
            List<KeyValuePair<string, int>> sortList = domains.ToList();
            sortList.Sort((firstPair, nextPair) =>
                {
                    return firstPair.Value.CompareTo(nextPair.Value);
                }
            );
            if (sortList.Count > 0) label5.Text = sortList[sortList.Count - 1].Value + " " + sortList[sortList.Count - 1].Key;
            else label5.Text = 0 + " null";
            if (sortList.Count > 1) label6.Text = sortList[sortList.Count - 2].Value + " " + sortList[sortList.Count - 2].Key;
            else label6.Text = 0 + " null";
            if (sortList.Count > 2) label7.Text = sortList[sortList.Count - 3].Value + " " + sortList[sortList.Count - 3].Key;
            else label7.Text = 0 + " null";

            if (sortList.Count > 0) label8.Text = sortList[0].Value + " " + sortList[0].Key;
            else label8.Text = 0 + " null";
            if (sortList.Count > 1) label9.Text = sortList[1].Value + " " + sortList[1].Key;
            else label9.Text = 0 + " null";
            if (sortList.Count > 2) label10.Text = sortList[2].Value + " " + sortList[2].Key;
            else label10.Text = 0 + " null";
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
        }

        private void tabPage2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void tabPage2_Enter(object sender, EventArgs e)
        {
            // calculate domain
            Dictionary<string, int> domains = new Dictionary<string, int>();
            for (int i = 0; i < rootForm.totalUrls.Count; i++)
            {
                int s = rootForm.totalUrls[i].IndexOf("//");
                if (s == -1) continue;
                s += 2;
                int ei = rootForm.totalUrls[i].IndexOf("/", s);
                if (ei == -1) ei = rootForm.totalUrls[i].Length;
                string domain = rootForm.totalUrls[i].Substring(s, ei - s);
                if (domains.ContainsKey(domain))
                    domains[domain]++;
                else
                    domains.Add(domain, 1);
            }

            List<KeyValuePair<string, int>> sortList = domains.ToList();
            sortList.Sort((firstPair, nextPair) =>
            {
                return firstPair.Value.CompareTo(nextPair.Value);
            }
            );

            for (int i = sortList.Count - 1; i >= 0; i--)
            {
                richTextBox1.AppendText(sortList[i].Value + " " + sortList[i].Key + "\n");
            }
        }
    }
}
